package com.liyu.rabbit.consumer;

import com.liyu.rabbit.factory.RabbitFactory;
import com.liyu.rabbit.producer.TopicProducer;
import com.rabbitmq.client.Channel;

/**
 * @author 李誉
 * @date 2022-06-21 21:51
 * @description 消费者
 */
public class Consumer {
    public static void main(String[] args) throws Exception{
        // 创建信道
        Channel channel = RabbitFactory.createNewChannel();


        channel.basicConsume(TopicProducer.HELLO_QUEUE, false, (consumerTag, message) -> {
            System.out.println("队列 -"+ TopicProducer.HELLO_QUEUE + "- 消费一条消息：" + new String(message.getBody()));
            // 手动应答，第二个参数控制是否批量应答
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        }, (consumerTag) -> {
            System.out.println("队列 -"+ TopicProducer.HELLO_QUEUE  + "- 消费一条消息" + consumerTag + "失败：");
        });


        channel.basicConsume(TopicProducer.HELLO_QUEUE2, false, (consumerTag, message) -> {
            System.out.println("队列 -"+ TopicProducer.HELLO_QUEUE2 + "- 消费一条消息：" + new String(message.getBody()));
            // 手动应答，第二个参数控制是否批量应答
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        }, (consumerTag) -> {
            System.out.println("队列 -"+ TopicProducer.HELLO_QUEUE2  + "- 消费一条消息失败：");
        });

    }
}
